$().ready(function() {
	$('.setting-btn').click(function(){
		//settings();
		switchwindow("settings");
	});
});
function settings(){
	$('.box-two .title span').text("Settings");
	show_box_settings();
}
function show_box_settings(){	
	strImgLink = $.cookie('strImgLink');	
	html="";
	html+='<div class="wrapper-settings img-rounded">';
		html+='<div class="wrapper-settings-list">';
			html+='<div class="setting-outer-border img-rounded">';
				html+='<div class="setting-mid-border img-rounded">';
					html+='<div class="settings-inner-border img-rounded">';
						html+='<ul class="settings-list">';
							html+='<li>';
								html+='<div class="settings-text-border">';
									html+='<div class="settings-text-border-inner">';
										html+='<div class="settings-text-border-label">';
											html+='<div class="pull-left">';
												html+='<a class="sign-out-btn">Sign Out</a>';
											html+='</div>';
											html+='<i class="clearfix"></i>';
										html+='</div>';
									html+='</div>';
								html+='</div>';
							html+='</li>';
							html+='<li>';
								html+='<div class="settings-text-border">';
									html+='<div class="settings-text-border-inner">';
										html+='<div class="settings-text-border-label">';
											html+='<div class="pull-left">';
												html+='<a id="fileupload-user-points"  class="fileinput-button" title="Upload image">';
													html+='Change Photo';
													html+='<input type="file" name="files" class="fileupload" id="input_fileupload"/>';
												html+='</a>';
											html+='</div>';
											html+='<div class="img-rounded">';
												html+='<i class="user-change-photo img-rounded"><img class="img-rounded" style="height:58px !important" src="'+base_url+"assets/uploaded/user_img/thumbnail/"+strImgLink+'" /></i>';
											html+='</div>';
											html+='<i class="clearfix"></i>';
										html+='</div>';
									html+='</div>';
								html+='</div>';
							html+='</li>';
						html+='</ul>';
					html+='</div>';
				html+='</div>';
			html+='</div>';
		html+='</div>';
	html+='</div>';
	$('.box-two-content').html(html);
	bind_func();
	set_top_user_fileupload();
}
function bind_func(){
	$('.sign-out-btn').click(function(){
	    $( "#box-wrapper-message" ).dialog({
	    	title: "Sign Out",
	    	autoOpen: false,
			height: 140,
			modal: true,
			draggable: false ,
			show: {
				effect: "explode",
				duration: 300
			},
			hide: {
				effect: "explode",
				duration: 300
			},	      
			buttons: {
				"Yes": function() {
					signoff();
					$(this).dialog("close");
				},
				"No": function() {
					$(this).dialog("close");
				}
			}
	      });
	    $('.ui-dialog-content').text("Are you sure?");
		$( "#box-wrapper-message" ).dialog("open");
	});
}
function signoff(){
	$('.back-btn a').click();
	$.cookie('owner', null);
	$.cookie('chk_log', null);
	$.cookie('session_code', null);
	$.cookie('dateConceptionDate', null);
	$('.h-wrapper-box').hide();
	$('.box-wrapper-icons').hide();
	$('.box-wrapper-splash').show();
	$('.splash-box').show();
	setTimeout(function(){ 
				$('.splash-box').fadeOut("slow",function(){
					$(".box-wrapper-splash").hide();
					$('.box-wrapper-login').show();
					$('.login-box').fadeIn("slow", function(){
						$('.box-wrapper-icons').show();
					});
				});
			},
			1000);
}
function set_top_user_fileupload(){
	$('#input_fileupload').attr('data-url',base_url+'assets/lib/jquery.fileupload/server/php/user.php');
	file_upload_top_user()
}
function file_upload_top_user(){
	$('.fileupload').fileupload();
	$('.fileupload').fileupload('option',{
		dataType: 'json',
		maxFileSize: 20000000, //20MB
		acceptFileTypes: /(\.|\/)(gif|jpe?g|png)$/i,			
		done: function (e, data) {
			$.each(data.result, function (index, file) {
				$.each(file, function (k, v) {		
					var intUserID = $.cookie('session_code');

					xfunc = "users/update_user_photo";
					data_sent = {
							'intUserID':intUserID,
							'strImgLink':v.name
					};
					
					$.ajax({ 
						type: "POST", // request type POST
						url: base_url+xfunc, // post to PHP file
						data: data_sent, // request send to PHP file
						dataType:'json',
						success: function(result){ // result = return from php request
							html = '<img class="img-rounded" style="height:58px !important" src="'+base_url+"assets/uploaded/user_img/thumbnail/"+v.name+'" />';
							$(".user-change-photo").html(html);	
							$.cookie('strImgLink',v.name);							
							$('.wrapper-settings').append('<div class="result-message-box"></div>');
						    $( ".result-message-box" ).dialog({
						    	title: "Upload Photo!",
						    	autoOpen: false, 
						    	minHeight: 140,
								modal: true,
								draggable: false ,
								show: {
									effect: "explode",
									duration: 300
								},
								hide: {
									effect: "explode",
									duration: 300
								},	      
								buttons: {
									"Ok": function() {
										$(this).dialog("close");
										$( ".result-message-box" ).remove();
									}
								}
						      });
						    $('.ui-dialog-content').text(result.message);
							$( ".result-message-box" ).dialog("open");	
						}
					});						
				});
			});
		}
	}).bind('fileuploadfailed', function (e, data) {
		$('.user-info-box').append('<div class="result-message-box"></div>');
	    $( ".result-message-box" ).dialog({
	    	title: "Upload Error!",
	    	autoOpen: false,
	    	minHeight: 140,
			modal: true,
			draggable: false ,
			show: {
				effect: "explode",
				duration: 300
			},
			hide: {
				effect: "explode",
				duration: 300
			},	      
			buttons: {
				"Ok": function() {
					$(this).dialog("close");
					$( ".result-message-box" ).remove();
				}
			}
	      });
	    $('.ui-dialog-content').text(data.errorThrown);
		$( ".result-message-box" ).dialog("open");		
	});		
}
